/**
 * @file models/myAttention.js
 * 我的关注
 * @author
 */

 import { routerRedux } from 'dva/router';
 import api from '../api';

 export default {
    namespace: "myattention",
    state:{
      groupName:'',
      groupNumber:'',
      username:'',
      MAcolumnKpi:['productName','fundCode', 'unitnav', 'fundManager', 'investStrategy', 'sharpeRatio', 'annulVolatility'],
      edit:1,
      isPublic:"Y",
      filterProduct:[],//分组筛选结果
      selectedRowKeys:[],//表格数据选择
      del:false,
      MpageSize:10,
      Mcurrent:1
    },
   subscriptions:{
     setup({dispatch,history}) {
       history.listen(({pathname,query}) => {
         if( pathname === '/fundScreen/myAttention'  ){
           dispatch({
             type: 'getPolicyGroup'
           })
           // dispatch({
           //   type:'clearState',
           //
           // })
          }
       })
     }
   },
   effects:{
     * getPolicyGroup({ payload }, { call, put ,select}) {
       console.log("getPolicyGroup>>>",payload  );
        const response = yield call(api.getPolicyGroup, payload);
        const state=yield select(state => state.myattention);
        console.log("getPolicyGroup>>",state.del);
        if(state.del == true){
          state.username = ''
          state.groupName = ''
        }

        yield put({
          type: 'getPolicyGroupSuccess',
          payload: { response },
        });
      },
     * getGroupDate({payload},{call,put}) {
       const groupNumber = payload.groupNumber;
        const response = yield call(api.getGroupDate,groupNumber);
        yield put({
          type: 'getGroupDateSuccess',
          payload: { response },
       });
     },
     * getfilterProduct({payload},{call,put,select}) {
       console.log("getfilterProduct>>>",payload);
       const groupNumber = payload.groupNumber;
       const response = yield call(api.getfilterProduct,groupNumber);
       const state=yield select(state => state.myattention)
       state.username = payload.username;
        yield put({
          type: 'getfilterProductSuccess',
          payload: { response },
        });
        yield put({
          type:'setState',
          payload:{
            selectedRowKeys:[]
          }
        })

      },
     * addPolicyGroup({payload},{call,put}){
        const response = yield call(api.addPolicyGroup, payload);
        yield put({
          type: 'getPolicyGroup',
        })
     },
     * updatePolicyGroup({payload},{call,put,select}){
       console.log("updatePolicyGroup>>",payload);
       const groupNumber = payload.groupNumber;
       const response = yield call(api.updatePolicyGroup,groupNumber,payload);
       const state=yield select(state => state.myattention)
       state.groupName = payload.groupName
       state.isPublic = payload.isPublic
       yield put({
         type: 'updateSuccess',
         payload:{
           isPublic:payload.isPublic,
           groupName:payload.groupName
         }
       });
       yield put({
         type: 'getPolicyGroup',
       })
       yield put({
         type:'setState',
         payload:{
           del:false
         }
       })
       yield put({
          type:'clearState',
       })
     },
     * deletePolicyGroup({payload},{call,put}){
       const groupNumber = payload.groupNumber;
       const response = yield call(api.deletePolicyGroup,groupNumber);
       yield put({
         type: 'getPolicyGroup',
       });
       yield put({
         type:'setState',
         payload:{
           del:true
         }
       })
     },
     * cancelAttention({ payload }, { select,call, put }) {
       console.log("cancelAttention>>",payload);
       const pdtid = payload.groupNumber;
       const username = payload.username;
       console.log("cancelAttention>>",username);
        const response = yield call(api.cancelAttention, payload);
        const state=yield select(state => state.myattention)
        state.username = payload.username;
        yield put({
          type: 'getfilterProduct',
          payload:{
            groupNumber:pdtid,
            username:state.username
          }
        });
      },
      *MAchangeColumns({payload:query},{select,put}){
        // console.log("changeColumns",query);
        const state=yield select(state => state.myattention)
        state.MAcolumnKpi = query
        if(_.isEmpty(query)){
          query = ['productName','fundCode', 'unitnav', 'fundManager', 'investStrategy', 'sharpeRatio', 'annulVolatility']
          state.MAcolumnKpi = query
        }else{
          state.MAcolumnKpi = query
        }
        yield put({
  				type: 'setState',
  				payload:{
            MAChangecolumns:true
          }
  			});
  		},
   },

   reducers:{
     setState(state, {payload}) {
      console.log("------",payload);
       return {
         ...state,
         ...payload,
       }
     },
     clearState(state) {
       return{
 				...state,
        isPublic:"Y",

 			}
     },
     getPolicyGroupSuccess(state, {payload}) {
       const {data} = payload.response
       console.log("getPolicyGroupSuccess>>>",payload.response,'111',data);
       const status=data ? true:false
       return {
         ...state,
         GroupStatus:status,
         GroupList:data,
       }
     },
     getGroupDateSuccess(state, {payload}) {
       const {data} = payload.response
       console.log("getGroupDateSuccess>>>",payload.response,'111',data);
       const status=data ? true:false
       return {
         ...state,
         GroupDataList:data,
         isPublic:data.isPublic
       }
     },
     getfilterProductSuccess(state, {payload}) {
       const {data} = payload.response
       console.log("getfilterProductSuccess>>>",state,'111',data);
       const status=data ? true:false
       return {
         ...state,
         filterProduct:data,
       }
     },
     updateSuccess(state, action) {
       console.log("updatePolicyGroup>>",state);
       return {
         ...state,
       };
     },
    }
 }
